using Microsoft.Extensions.DependencyInjection;
using Microsoft.VisualStudio.TestTools.UnitTesting;

using System;

using zijian666.DI;

namespace UnitTests;

[TestClass]
public class 循环引用中的构造函数装配
{
    [TestMethod]
    public void Test()
    {
        var services = new ServiceCollection()
            .AddSingleton<MyService1>()
            .AddSingleton<MyService2>()
            .AddAutowired()
            .BuildServiceProvider();


        var mysrv = services.GetService<MyService1>();
        Assert.IsNotNull(mysrv);
        Assert.IsNotNull(mysrv.MyService2);
        Assert.AreEqual(mysrv.MyService2.MyService1, mysrv);
        Assert.AreEqual(mysrv.MyService, mysrv);
        Assert.AreEqual(mysrv.MyService2.MyService, mysrv.MyService2);

    }



    class MyService1
    {
        public MyService1(IServiceProvider services)
        {
            services.Autowired(() => this);
        }
        [Autowired]
        public MyService2 MyService2;
        [Autowired]
        public MyService1 MyService;

    }

    class MyService2
    {
        public MyService2(IServiceProvider serviceProvider)
        {
            serviceProvider.Autowired(() => this);
        }
        [Autowired]
        public readonly MyService1 MyService1;
        [Autowired]
        public readonly MyService2 MyService;
    }
}
